import java.io.FileReader;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;

public class PopulationMain {

	public static final String THUMBNAILS_URL = "MiviaMovieServer/thumbnails/";
	public static final String TRAILERS_URL = "MiviaMovieServer/trailers/";
	public static final String PLAYBILLS_URL = "MiviaMovieServer/playbills/";

	public static void main(String[] args) {
		deleteMovies();
		populateMovies(readMovies("Films.csv"));
	}

	private static void deleteMovies() {
		MySQLConnection connection;
		try {
			connection = new MySQLConnection("root", "root", "MiviaMovieDB");

			PopulationQuery.deleteMovies(connection);
			connection.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void populateMovies(List<DBMovie> movies) {
		MySQLConnection connection;
		try {
			connection = new MySQLConnection("root", "root", "MiviaMovieDB");

			for (DBMovie movie : movies)
				PopulationQuery.insertDetailedMovie(connection, movie);
			connection.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static List<DBMovie> readMovies(String filename) {
		Reader reader;
		List<DBMovie> movies = null;
		try {
			reader = new FileReader(filename);
			CSVReader<String[]> csvParser = CSVReaderBuilder
					.newDefaultReader(reader);

			List<String[]> data = csvParser.readAll();
			movies = new ArrayList<DBMovie>();

			DBMovie movie = null;

			for (String[] v : data) {

				if (!v[0].equals("")) {
					movie = new DBMovie();
					movie.setTitle(v[0]);
					movie.setDate(stringToDate(v[1], "yyyy-mm-dd"));
					movie.setDirector(new Person(v[2], v[3]));
					movie.getActors().add(new Person(v[4], v[5]));
					movie.getMovieTypes().add(new MovieType(v[6]));
					movie.setThumbnailURL(THUMBNAILS_URL + v[7] + ".png");
					movie.setPlaybillURL(PLAYBILLS_URL + v[7] + ".png");
					movie.setTrailerURL(TRAILERS_URL + v[7] + ".mp4");
					movies.add(movie);
				} else {
					if (!v[4].equals(""))
						if (movie != null)
							movie.getActors().add(new Person(v[4], v[5]));
					if (!v[6].equals(""))
						if (movie != null)
							movie.getMovieTypes().add(new MovieType(v[6]));
				}

			}

			for (DBMovie m : movies)
				System.out.println(m.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return movies;
	}

	private static Date stringToDate(String dateString, String format) {
		Date date = null;
		try {
			java.util.Date utilDate = new SimpleDateFormat(format)
					.parse(dateString);
			date = new Date(utilDate.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
}
